package model

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"go-member/db"
	"strconv"
)

type Category struct {
	Id         uint
	Money      string
	Number     uint
	Note       string
	Title      string
	Created_at string
	Updated_at string
	Deleted_at string
}

// AddCategory
// @Description: 添加结构体
type AddCategory struct {
	Money      string
	Number     uint
	Note       string
	Created_at string
	Updated_at string
}

type CategoryStruct struct {
	List        interface{}
	Total       uint
	CurrentPage uint
}

func (Category) TableName() string {
	return "category"
}

// AfterFind
// @Description: 钩子-重置标题
// @receiver category
func (category *Category) AfterFind() {
	category.Title = fmt.Sprintf("%s 元 %d 次", category.Money, category.Number)
}

// GetCageGoryById
// @Description: 通过id获取分类数据
// @param id
// @return any
func GetCageGoryById(id any) (Category, bool) {
	mysql := db.InitMysql()
	category := Category{}
	e := mysql.Where("id= ?", id).Debug().First(&category).Error
	if e == gorm.ErrRecordNotFound {
		return category, false
	}
	return category, true
}

// ExistsCategory
// @Description: 查询分类数据是否存在
// @param val
// @return bool
func ExistsCategory(val string) bool {
	mysql := db.InitMysql()
	category := Category{}
	id, _ := strconv.Atoi(val)
	e := mysql.Where("id= ?", id).Debug().First(&category).Error
	if e == gorm.ErrRecordNotFound {
		return false
	}
	return true
}
